package com.example.pucha_xian.Activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.pucha_xian.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends BaseActivity {

    private static final int READ_CONTACTS_REQUEST =1;
    private Context context;
    private Timer mTimer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        permission();
        context=this;
    }

    private void init() {
        mTimer=new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    toMain();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },3*1000 );
    }

    private void toMain() throws IOException, JSONException {
        SharedPreferences preferences = getSharedPreferences("PuCha", Context.MODE_PRIVATE);
        //String code =getVersion();
//        if ("1.2.2".equals(code)){
            if(preferences.contains("token")){
                Intent intent=new Intent(this,ArcgisActivity.class);
                startActivity(intent);
            } else{
                Intent intent=new Intent(this, LoginActivity.class);
                // Intent intent=new Intent(this,SuperMapActivity.class);
                startActivity(intent);
            }
//        }else {
//            //解决在子线程中调用Toast的异常情况处理
//            Looper.prepare();
//            Toast.makeText(context,"请联系管理员更新系统", Toast.LENGTH_LONG).show();
//            Looper.loop();
//        }

    }

    /**
     * 获取最新版本信息
     * @return
     * @throws IOException
     * @throws JSONException
     */
    private String getVersion() throws IOException, JSONException {
        URL url = new URL("http://oa.xcyy.net.cn:8089/version.txt");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setReadTimeout(8 * 1000);
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String string;
        string = bufferedReader.readLine();
        //对json数据进行解析
        JSONObject jsonObject = new JSONObject(string);
        String strings = jsonObject.getString("code");
        return strings;
    }

    private void permission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            //权限为获取，检查用户是否被询问过并且拒绝了，如果是这样的话，给予更多
            //解释
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {
                Toast.makeText(this, "需要读取权限", Toast.LENGTH_SHORT).show();
            }
            //发起请求获得用户许可,可以在此请求多个权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE}, READ_CONTACTS_REQUEST);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        //确保是我们的请求
        if (requestCode == READ_CONTACTS_REQUEST) {
            if (grantResults.length == 6 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               // Toast.makeText(this, "CAMERA获取权限成功", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "CAMERA获得权限失败", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
              //  Toast.makeText(this, "WRITE_EXTERNAL_STORAGE获取权限成功", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[1] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "WRITE_EXTERNAL_STORAGE获得权限失败", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
              //  Toast.makeText(this, "READ_EXTERNAL_STORAGE获取权限成功", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[2] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "READ_EXTERNAL_STORAGE获得权限失败", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[3] == PackageManager.PERMISSION_GRANTED) {
             //   Toast.makeText(this, "ACCESS_COARSE_LOCATION获取权限成功", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[3] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "ACCESS_COARSE_LOCATION获得权限失败", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[4] == PackageManager.PERMISSION_GRANTED) {
              //  Toast.makeText(this, "ACCESS_FINE_LOCATION获取权限成功", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[4] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "ACCESS_FINE_LOCATION获得权限失败", Toast.LENGTH_SHORT).show();
            }else if (grantResults.length == 6 && grantResults[5] == PackageManager.PERMISSION_GRANTED) {
              //  Toast.makeText(this, "READ_PHONE_STATE获取权限成功", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length == 6 && grantResults[5] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "READ_PHONE_STATE获得权限失败", Toast.LENGTH_SHORT).show();
            }
            init();
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
